import React from 'react';

import Config from '../config';
import Constants from '../common/Constants';
import WorkHardModel from './WorkHardModel';
import LoginModel from './LoginModel';
import UserModel from './UserModel';
import ActivityModel from './ActivityModel';
import CommonModel from './CommonModel';
import AliModel from './AliModel';
import NewsModel from './NewsModel';
import HeartModel from './HeartModel';
import SportModel from './SportModel';
import GuahaoModel from './GuahaoModel';
import QuanZiModel from './QuanZiModel';
import IMModel from './IMModel';
import VipSerCardModel from './VipSerCardModel';
import ZahuopuModel from './ZahuopuModel';
import PostModel from './PostModel';
import JifenMallModel from './JifenMallModel';
import PushModel from './PushModel';
import ShuwuModel from './ShuwuModel';
import NoticeModel from './NoticeModel';
import JianyiModel from './JianyiModel';
var instance = null;

export class DataManager {
  constructor() {
    if (!instance) {
      instance = this;
      console.log('constructor DataManager');
    }
    return instance;
  }

  setup(callback) {
    console.log('setup DataManager');
    this._callbackData = {};
    this.callback = callback;
    this.setupModel();
  }

  setupModel() {
    this.workCount = 0;

    this.modelDic = {};
    this.modelDic[Constants.MODULE_TYPE.WORK_HARD] = new WorkHardModel();
    this.modelDic[Constants.MODULE_TYPE.WORK_HARD].setup(this);

    this.workCount++;
    this.modelDic[Constants.MODULE_TYPE.LOGIN] = new LoginModel();
    this.modelDic[Constants.MODULE_TYPE.LOGIN].setup(this._callByLogin.bind(this), this);

    this.workCount++;
    this.modelDic[Constants.MODULE_TYPE.USER] = new UserModel();
    this.modelDic[Constants.MODULE_TYPE.USER].setup(this, this._callByUser.bind(this));

    this.modelDic[Constants.MODULE_TYPE.ACTIVITY] = new ActivityModel();
    this.modelDic[Constants.MODULE_TYPE.ACTIVITY].setup(this);

    this.workCount++;
    this.modelDic[Constants.MODULE_TYPE.COMMON] = new CommonModel();
    this.modelDic[Constants.MODULE_TYPE.COMMON].setup(this, this._callByCommon.bind(this));

    this.modelDic[Constants.MODULE_TYPE.ALI_OSS] = new AliModel();
    this.modelDic[Constants.MODULE_TYPE.ALI_OSS].setup(this);

    this.modelDic[Constants.MODULE_TYPE.NEWS] = new NewsModel();
    this.modelDic[Constants.MODULE_TYPE.NEWS].setup(this);

    this.modelDic[Constants.MODULE_TYPE.HEART] = new HeartModel();
    this.modelDic[Constants.MODULE_TYPE.HEART].setup(this);

    this.modelDic[Constants.MODULE_TYPE.VIPSERCARD] = new VipSerCardModel();
    this.modelDic[Constants.MODULE_TYPE.VIPSERCARD].setup(this);

    this.modelDic[Constants.MODULE_TYPE.GUAHAO] = new GuahaoModel();
    this.modelDic[Constants.MODULE_TYPE.GUAHAO].setup(this);

    this.modelDic[Constants.MODULE_TYPE.QUAN_ZI] = new QuanZiModel();
    this.modelDic[Constants.MODULE_TYPE.QUAN_ZI].setup(this);

    this.modelDic[Constants.MODULE_TYPE.ZAHUOPU] = new ZahuopuModel();
    this.modelDic[Constants.MODULE_TYPE.ZAHUOPU].setup(this);

    this.modelDic[Constants.MODULE_TYPE.POST] = new PostModel();
    this.modelDic[Constants.MODULE_TYPE.POST].setup(this);

    this.modelDic[Constants.MODULE_TYPE.JIFENMALL] = new JifenMallModel();
    this.modelDic[Constants.MODULE_TYPE.JIFENMALL].setup(this);

    this.modelDic[Constants.MODULE_TYPE.PUSH] = new PushModel();
    this.modelDic[Constants.MODULE_TYPE.PUSH].setup(this);

    this.modelDic[Constants.MODULE_TYPE.SHUWU] = new ShuwuModel();
    this.modelDic[Constants.MODULE_TYPE.SHUWU].setup(this);

    this.modelDic[Constants.MODULE_TYPE.NOTICE] = new NoticeModel();
    this.modelDic[Constants.MODULE_TYPE.NOTICE].setup(this);

    this.modelDic[Constants.MODULE_TYPE.JIANYI] = new JianyiModel();
    this.modelDic[Constants.MODULE_TYPE.JIANYI].setup(this);
  }

  getModel(moduleType) {
    return this.modelDic[moduleType];
  }

  callMethod(moduleType, method, params) {
    return this.modelDic[moduleType][method].apply(null, params);
  }

  _callBySport() {
    this._dealCallback();
  }

  _callByUser() {
    this.modelDic[Constants.MODULE_TYPE.PUSH].initAlias();

    this.workCount++;
    this.modelDic[Constants.MODULE_TYPE.SPORT] = new SportModel();
    this.modelDic[Constants.MODULE_TYPE.SPORT].setup(this, this._callBySport.bind(this));

    this.workCount++;
    this.modelDic[Constants.MODULE_TYPE.IM] = new IMModel();
    this.modelDic[Constants.MODULE_TYPE.IM].setup(this, this._callByIM.bind(this));

    this._dealCallback();
  }

  _callByIM() {
    this._dealCallback();
  }

  _callByLogin() {
    this._dealCallback();
  }

  _callByCommon(versionData) {
    this._callbackData.version = versionData;
    this._dealCallback();
  }

  _dealCallback() {
    this.workCount = this.workCount - 1;
    this._checkCallback();
  }

  _checkCallback() {
    if (this.workCount == 0) {
      this.callback(this._callbackData);
    }
  }
}
new DataManager();
module.exports = instance;
